package com.jasonjan.jiance.module.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jasonjan.jiance.R;
import com.jasonjan.jiance.banner.CycleViewPager;
import com.jasonjan.jiance.banner.MyImage;
import com.jasonjan.jiance.banner.ViewFactory;
import com.jasonjan.jiance.global.InitApp;
import com.jasonjan.jiance.module.base.BasePagerAdapter;
import com.jasonjan.jiance.module.home.listview.HomeTitleView;
import com.jasonjan.jiance.util.SettingUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JasonJan on 2017/12/1.
 */

public class HomeTabLayout extends Fragment {
    private static final String TAG = "HomeTabLayout";
    private static HomeTabLayout instance = null;
    private static int pageSize = InitApp.AppContext.getResources().getStringArray(R.array.photo_id).length;
    private String categoryId[] = InitApp.AppContext.getResources().getStringArray(R.array.photo_id);
    private String categoryName[] = InitApp.AppContext.getResources().getStringArray(R.array.photo_name);
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private BasePagerAdapter adapter;

    //轮播图
    ArrayList<String> stringArrayList=new ArrayList<>();
    private CycleViewPager cycleViewPager=null;
    private ArrayList<ImageView> views=new ArrayList<>();
    private ArrayList<MyImage> myImageArrayList=new ArrayList<>();


    public static HomeTabLayout getInstance() {
        if (instance == null) {
            instance = new HomeTabLayout();
        }
        return instance;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_tab, container, false);
        initView(view);
        initAD();
        initData();
        return view;
    }

    /**
     * 初始化广告页
     */
    public void initAD(){
        stringArrayList.add("http://www.jiancenj.com/Files/ImgUpload/1eb672ee2d444cc181389cc7383e64ae.jpg");
        stringArrayList.add("http://www.jiancenj.com/Files/ImgUpload/29566075efc64ebd8b9f8d5d56d33754.jpg");
        stringArrayList.add("http://www.jiancenj.com/Files/ImgUpload/7ab24faf0ff140e1a25fd810f50502ad.jpg");
        cycleViewPager = (CycleViewPager) getActivity().getFragmentManager().findFragmentById(R.id.oneFm_fragment_cycle_viewpager_content);
        views.add(ViewFactory.getImageView(getContext(), stringArrayList.get(stringArrayList.size()-1))); // 将最后一个ImageView添加进来
        for (int i=0;i<stringArrayList.size();i++) {
            MyImage myImage=new MyImage();
            myImage.setImage(stringArrayList.get(i));
            myImage.setLink("");
            myImage.setTitle("");
            myImageArrayList.add(myImage);
            views.add(ViewFactory.getImageView(getContext(), stringArrayList.get(i)));
        }
        views.add(ViewFactory.getImageView(getContext(), stringArrayList.get(0))); // 将第一个ImageView添加进来
        cycleViewPager.setCycle(true);// 设置循环，在调用setData方法前调用
        cycleViewPager.setData(views,myImageArrayList , new CycleViewPager.ImageCycleViewListener() {
            @Override
            public void onImageClick(MyImage info, int postion, View imageView) {

            }
        }); // 在加载数据前设置是否循环
        cycleViewPager.setWheel(true); //设置轮播
        cycleViewPager.setTime(3000); // 设置轮播时间
        cycleViewPager.setIndicatorCenter(); //设置圆点指示图标组居中显示，默认靠右
    }



    @Override
    public void onResume() {
        super.onResume();
        tabLayout.setBackgroundColor(SettingUtil.getInstance().getColor());
    }


    private void initView(View view){
        tabLayout = view.findViewById(R.id.tab_layout_photo);
        viewPager = view.findViewById(R.id.view_pager_photo);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setBackgroundColor(SettingUtil.getInstance().getColor());
        viewPager.setOffscreenPageLimit(pageSize);
    }

    private void initData(){

        Fragment fragment = HomeTitleView.newInstance("news");//这里需要更改
        fragmentList.add(fragment);

        Fragment fragment2= HomeTitleView.newInstance("lesson");//预约课程
        fragmentList.add(fragment2);

        adapter = new BasePagerAdapter(getChildFragmentManager(), fragmentList, categoryName);
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onDestroy() {
        if (instance != null) {
            instance = null;
        }
        super.onDestroy();
    }
}
